﻿define(function (require, exports, module) {
    var ImageStorage = require('imageStorage');
    var Canvas = require('canvas');
    var m_loginCanvas = Canvas.Get();
    var m_form, m_txtName, m_txtPwd, m_btnLogin;

    exports.Show = eval(Wind.compile('async', function () {
        var img = $await(ImageStorage.GetAsync('images/other/login.gif'));
        m_loginCanvas.Call('drawImage', img, 0, 0);
        m_form = $('#mainForm').attr('action', 'login.do');
        m_txtName = $('<input name="name" type="text">').css({
            border: 0,
            position: 'absolute',
            zIndex: 5,
            top: 229,
            left: 397,
            width: 141,
            height: 20,
            fontSize: 17
        }).appendTo(m_form);
        m_txtPwd = $('<input name="password" type="password">').css({
            border: 0,
            position: 'absolute',
            zIndex: 5,
            top: 266,
            left: 397,
            width: 141,
            height: 20,
            fontSize: 17
        }).appendTo(m_form);
        m_btnLogin = $('<img src="images/other/login_btn.gif">').css({
            position: 'absolute',
            zIndex: 5,
            top: 318,
            left: 320
        }).hide().appendTo(m_form);

        m_txtName.focus();
        m_loginCanvas.Bind(module.id, {
            mousemove: function (e) {
                m_btnLogin[342 <= e.clientX && e.clientX <= 468 && 330 <= e.clientY && e.clientY <= 392 ? 'show' : 'hide']();
            }
        });

        while (true) {
            $await(Wind.Async.onEvent(m_btnLogin[0], 'click'));
            if ('' == m_txtName.val().trim() || '' == m_txtPwd.val().trim()) {
                m_txtName.focus();
                continue;
            }
            var response = $await(FromAjaxAsync(m_form));
            if (response.Success)
                location.reload();
            else
                alert(result.Message);
        }
    }));

    function FromAjaxAsync(form, options) {
        return Wind.Async.Task.create(function (t) {
            form.ajaxSubmit({
                success: function (data, textStatus, jqXHR) {
                    t.complete('success', data);
                }
            });
        });
    };
});